/*
   Copyright 2010 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.latestbit.smartobjects.examples.chat.server;

import org.latestbit.smartobjects.core.ServerSkeleton;
import org.latestbit.smartobjects.examples.chat.*;
import org.latestbit.smartobjects.exceptions.SmartBusSystemException;

public class ChatSessionImpl implements IChatSessionSyncSmartObject {

	private ChatServiceImpl service;
	private String login;
	private IChatEvents eventsCallback;

	public ChatSessionImpl(
			ChatServiceImpl service, 
			String login, 
			IChatEvents eventCallback) {
		this.service = service;
		this.login = login;
		this.eventsCallback = eventCallback;
	}
	
	@Override
	public void destroy() {
		service.removeChatSession(this.login);
	}

	@Override
	public void sendMessage(ChatMessage message) throws SmartBusSystemException {
		service.sendMessageToAll(this, message);
	}

	@Override
	public void sendPrivateMessage(String login, ChatMessage message) throws SmartBusSystemException {
		service.sendPrivateMessage(this, login, message);
	}

	@Override
	public void leaveFromChat() throws SmartBusSystemException {
		ServerSkeleton.getCurrentRequestContext().getSkeleton().destroy();
	}

	public void setEventsCallback(IChatEvents eventsCallback) {
		this.eventsCallback = eventsCallback;
	}

	public IChatEvents getEventsCallback() {
		return eventsCallback;
	}

	public String getLogin() {
		return login;
	}

}
